package sample.plugin.http.lang.engine;

import com.rapid.api.lang.engine.CancellationToken;
import com.rapid.api.lang.engine.completion.CompletionItem;
import com.rapid.api.lang.engine.completion.CompletionItemKind;
import com.rapid.api.lang.engine.completion.CompletionParams;
import com.rapid.api.lang.engine.completion.CompletionProvider;
import com.rapid.api.lang.engine.completion.CompletionResult;

public class RequestCompletionProvider implements CompletionProvider {

    @Override
    public void complete(CompletionParams params, CompletionResult result, CancellationToken token) {
        String partial = params.partial;
        partial = partial.toLowerCase();
        if ("post".startsWith(partial)) {
            result.add(keyword("post", "请求类型"));
            result.add(snippet("post", "post($0网址)\nheader: {\n\t$0\n}\nparam: "));
        } else if ("put".startsWith(partial)) {
            result.add(keyword("put", "请求类型"));
        } else if ("get".startsWith(partial)) {
            result.add(keyword("get", "请求类型"));
        } else if ("delete".startsWith(partial)) {
            result.add(keyword("delete", "请求类型"));
        } else if ("param".startsWith(partial)) {
            result.add(keyword("param", "参数"));
        } else if ("header".startsWith(partial)) {
            result.add(keyword("header", "请求头"));
            result.add(snippet("header", "header: {\n\t$0\n}"));
        }
    }

    @Override
    public void resolve(CompletionItem completionItem) {

    }

    private CompletionItem keyword(String type, String desc) {
        CompletionItem item = new CompletionItem();
        item.label = type;
        item.detail = desc;
        item.insertText = type;
        item.kind = CompletionItemKind.Class;
        return item;
    }

    private CompletionItem snippet(String name, String content) {
        CompletionItem item = new CompletionItem();
        item.label = name;
        item.detail = "快捷片段";
        item.insertText = content;
        item.kind = CompletionItemKind.Snippet;
        return item;
    }
}
